//
//  LGFLoginImageTitleView.swift
//  MedicalUnionHybrid
//
//  Created by 来 on 2019/9/23.
//  Copyright © 2019 EWell. All rights reserved.
//

import UIKit

protocol LGFLoginImageTitleViewDelegate: class {
    func selectLGFLoginImageTitleView(_ view: LGFLoginImageTitleView)
}

class LGFLoginImageTitleView: UIView {

    weak var delegate: LGFLoginImageTitleViewDelegate?
    
    var isCanSelect: Bool!
    
    var imageView: UIImageView!
    var arrowImageView: UIImageView!
    var textField: UITextField!
    
    var left: Float = 0.0
    var image: UIImage!
    var arrow_image: UIImage!
    var text: String! {
        didSet {
            if textField != nil {
                textField.text = text
            }
        }
    }
    var placeholder: String!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init(_ image: UIImage!, _ arrow_image: UIImage!, left: Float, placeholder: String, text: String, _ isCanSelect: Bool) {
        super.init(frame: .zero)
        self.image = image
        self.arrow_image = arrow_image
        self.left = left
        self.text = text
        self.placeholder = placeholder
        if isCanSelect {
            self.lgf_AddTap(target: self, action: #selector(selectView(_:)))
        }
        configUI()
        self.isCanSelect = isCanSelect
        self.textField.isEnabled = !isCanSelect
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configUI() {
        
        imageView = UIImageView.init()
        imageView.image = self.image ?? UIImage.init()
        imageView.isHidden = self.image == nil
        addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.height.equalTo(16)
            make.width.equalTo(self.image == nil ? 0 : 14)
        }
        
        arrowImageView = UIImageView.init()
        arrowImageView.image = self.arrow_image ?? UIImage.init()
        addSubview(arrowImageView)
        arrowImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(15)
            make.width.equalTo(15)
        }
        
        textField = UITextField.init()
        textField.text = self.text
        textField.attributedPlaceholder = NSAttributedString.init(string:self.placeholder, attributes: [NSAttributedString.Key.foregroundColor:UIColor.init(lgf_HexString: "CFD4D9")!])
        textField.textColor = UIColor.init(lgf_HexString: "333333")
        textField.font = UIFont.systemFont(ofSize: 16)
        addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.left.equalTo(imageView.snp.right).offset(self.image == nil ? self.left : 20)
            make.centerY.equalToSuperview()
            make.height.equalToSuperview()
            make.right.equalTo(arrowImageView.snp.left)
        }
        
        // 底部线
        let line = UIView.init()
        line.backgroundColor = EWEDarkMode.color_F1F2F4()
        addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        
    }
    
    @objc func selectView(_ sender: UITapGestureRecognizer) -> Void {
        self.delegate?.selectLGFLoginImageTitleView(self)
    }

}
